import { ref } from 'vue'
import { defineStore } from 'pinia'
import { ServiceRole } from '@/manager/constants'
import bridge from '@/common/bridge'
import type { IUserData } from '@/common/types'
import { UserTypeEnum } from '@/common/constants'

export const useMessageEditorStore = defineStore('messageEditor', () => {
  const content = ref('')
  const serviceRole = ref(ServiceRole.SELF)

  const replaceContent = (text: string) => {
    content.value = text
  }

  const appendContent = (text: string) => {
    content.value += `${content.value ? ' ' : ''}${text}`
  }

  const appendFace = (key: string) => {
    const text = `face[${key}]`
    appendContent(text)
  }

  const clearContent = () => {
    content.value = ''
  }

  return { content, serviceRole, replaceContent, appendContent, appendFace, clearContent }
})

export const useLoginData = defineStore('loginData', () => {
  const loginData = ref<IUserData>({
    userId: '',
    userName: '',
    userAvatar: ''
  })

  const setLoginData = (data: IUserData) => {
    loginData.value = data
    bridge.register({
      userId: data.userId,
      userType: UserTypeEnum.SERVICE,
      merchantCode: '一号商户'
    })
  }

  return { loginData, setLoginData }
})
